package com.shareyi.baseproject.kernel.adpater.spi;

import com.shareyi.baseproject.kernel.adapter.spi.DefaultObjectSerialization;
import com.shareyi.baseproject.kernel.adapter.spi.sequence.DefaultSequenceProvider;
import com.shareyi.baseproject.kernel.common.spi.SpiHolder;
import com.shareyi.baseproject.kernel.common.spi.sequence.SequenceProvider;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * spi提供测试
 *
 * @author david
 * @date 2021-08-31
 */
public class SpiTest_UT {

    /**
     * 测试序列化工具
     */
    @Test
    public void testObjectSerialization() {
        DefaultObjectSerialization defaultObjectSerialization = new DefaultObjectSerialization();
        SpiHolder.getInstance().setObjectSerialization(defaultObjectSerialization);

        Map<String, Object> param = new HashMap() {
            {
                put("0", 0);
            }
        };
        String json = SpiHolder.getInstance().getObjectSerialization().toJSONString(param);
        Assertions.assertTrue(StringUtils.isNotEmpty(json));

        Map jsonMap = SpiHolder.getInstance().getObjectSerialization().parseObject(json, Map.class);
        Assertions.assertNotNull(jsonMap);
        Assertions.assertEquals(jsonMap.size(), 1);

        List<Integer> integerList = SpiHolder.getInstance().getObjectSerialization().parseArray("[1,2,3]", Integer.class);
        Assertions.assertNotNull(integerList);
        Assertions.assertEquals(integerList.size(), 3);
    }

    /**
     * 测试序号工具
     */
    @Test
    public void testSequenceProvider() {
        DefaultSequenceProvider defaultObjectSerialization = new DefaultSequenceProvider();
        defaultObjectSerialization.setClusterUniqueCheckEnable(false);
        SpiHolder.getInstance().setLongSequenceProvider(defaultObjectSerialization);

        Assertions.assertNotNull(SpiHolder.getInstance().getLongSequenceProvider().next(SequenceProvider.DEFAULT_TYPE));
    }

}
